<#assign currentTime = .now>
package ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.infrastructure.repository.impl;

import com.neegix.application.query.NebulaSQL;
import com.neegix.base.PageVO;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.application.assembler.${templateTable.javaTableName}Assembler;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.application.cqrs.query.${templateTable.javaTableName}QueryRepository;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.application.cqrs.query.condition.${templateTable.javaTableName}WhereGroup;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.application.dto.${templateTable.javaTableName}DTO;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.interfaces.vo.${templateTable.javaTableName}VO;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.infrastructure.repository.convert.${templateTable.javaTableName}Converter;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.infrastructure.repository.dataobject.${templateTable.javaTableName}DO;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.infrastructure.repository.mapper.${templateTable.javaTableName}Mapper;
import ${templateTable.packageName}.${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}.infrastructure.repository.mapper.customized.${templateTable.javaTableName}CustomizedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="${templateTable.copyright}">${templateTable.copyright}</a>.
  *
  * @author <a href="${templateTable.copyright}">${templateTable.copyright}</a>
  * @version ${templateTable.version}
  * @since ${currentTime?string("yyyy-MM-dd HH:mm:ss")}
  */

@Repository
public class ${templateTable.javaTableName}QueryRepositoryImpl implements ${templateTable.javaTableName}QueryRepository {

    @Autowired
    private ${templateTable.javaTableName}Mapper ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}Mapper;

    @Autowired
    private ${templateTable.javaTableName}CustomizedMapper ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}CustomizedMapper;

    @Override
    public Optional<${templateTable.javaTableName}VO> findById(Long id) {
        ${templateTable.javaTableName}DO ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DO = ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}Mapper.selectByPrimaryKey(id);
        return Optional.ofNullable(${templateTable.javaTableName}Assembler.INSTANCE.covertVO(${templateTable.javaTableName}Converter.INSTANCE.covertDTO(${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DO)));
    }

    @Override
    public Integer selectCount(List<Long> ids) {
        return ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}CustomizedMapper.selectCountByIds(ids);
    }

    @Override
    public PageVO<${templateTable.javaTableName}VO> findPage(Integer currentPage, Integer pageSize, ${templateTable.javaTableName}DTO ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DTO) {
        NebulaSQL nebulaSQL = new NebulaSQL();
        nebulaSQL.createWhereGroups(${templateTable.javaTableName}WhereGroup.class)<#if templateTable.columns?exists>
           <#list templateTable.columns as column>
               <#if column.fullyQualifiedJavaType?contains("Instant")>
           .and${column.javaName?cap_first}Between(${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DTO.getStart${column.javaName?cap_first}(),${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DTO.getEnd${column.javaName?cap_first}())<#if !column?has_next>;</#if>
               <#elseif column.fullyQualifiedJavaType?contains("String")>
           .and${column.javaName?cap_first}LikeTo(${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DTO.get${column.javaName?cap_first}())<#if !column?has_next>;</#if>
               <#else>
           .and${column.javaName?cap_first}EqualTo(${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}DTO.get${column.javaName?cap_first}())<#if !column?has_next>;</#if>
               </#if>
           </#list>
           <#else>;</#if>
        nebulaSQL.setPager(currentPage, pageSize);
        List<${templateTable.javaTableName}DO> result = ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}Mapper.selectList(nebulaSQL);
        Long total = ${templateTable.javaTableName[0]?lower_case+templateTable.javaTableName[1..]}Mapper.selectCount(nebulaSQL);
        PageVO<${templateTable.javaTableName}VO> page = new PageVO<>(currentPage, pageSize);
        page.setTotal(total);
        page.setResult(${templateTable.javaTableName}Assembler.INSTANCE.covertVO(${templateTable.javaTableName}Converter.INSTANCE.covertDTOS(result)));
        return page;
    }
}
